package com.web_bloks.widget.client;

import com.web_bloks.system.client.Logger;
import com.google.gwt.dom.client.Document;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.http.client.Response;

/**
 * 
 * @author Jean-Lou Dupont
 *
 */
public class WidgetManager {

	final static String thisClass = "com.web_bloks.widget.WidgetManager";
	
	static WidgetManager self = new WidgetManager();
	
	protected WidgetManager() {
		super();
	}
	
	public static WidgetManager create() {
		//Logger.logInfo(thisClass+".create self["+self+"]");
		
		return self;
	}
	
	/*****************************************
	 * 
	 *****************************************/
	public void createAllFromNode(String collectionName, Panel panel, Element el) {
		WidgetFactory.createAllFromNode(collectionName, panel, el);
	}
	
	/*****************************************
	 * 
	 *****************************************/
	public void executeListeners(WidgetEx source, String listeners, String cond, Response response) {

		if ( null == source )
			throw new RuntimeException(thisClass+".executeChilds: source can't be null");
		
		String[] liste = extractListenersList(listeners);
		if (null == liste)
			return;
		
		String collectionName = source.getOriginCollection();
		com.google.gwt.dom.client.Element el = null;
		Document doc = Document.get();
		WidgetEx w = null;
		
		for (int i=0;i<liste.length;i++) {
			el = doc.getElementById(liste[i]);
			if ( null!=el ) {
				w = findWidget( collectionName, el);
				if (null!=w) {
					if ( w instanceof WidgetCommand ) {
						//Window.alert("WidgetManager.executeChilds: EXECUTING, id=" + w.getId());
						//Logger.logInfo(thisClass+".executeChilds, EXECUTING");
						((WidgetCommand) w).executeWidgetCommand( (Widget)source, cond, response );
					}
				}
			}
		}
		
	}//
	protected String[] extractListenersList(String listeners) {
		
		if ( null == listeners )
			return null;
		
		return listeners.split(",");
	}
	public WidgetEx findWidget(String collectionName, com.google.gwt.dom.client.Element source) {
		
		if ( null == source )
			throw new RuntimeException(thisClass+".findWidget: source can't be null");

		String id = null;
		//id = DOM.getElementProperty(source, "id" );
		id = source.getAttribute("id");

		if ( null == id || id.length() == 0 ) {
			Logger.logInfo( thisClass+".findWidget: widget without ID detected");
			return null;
		}
		
		WidgetCollection c = WidgetCollection.create(collectionName);
		
		return c.getWidget(collectionName, id);
	}

	public void add(String collectionName, String key, WidgetEx w) {
		Logger.logInfo( thisClass+".add: collectionName["+collectionName+"] key["+key+"]");
		WidgetCollection c = WidgetCollection.create( collectionName );
		
		c.addWidget(collectionName, key, w);
	}
	
	public void clear(String collectionName) {
		WidgetCollection c = WidgetCollection.create(collectionName);
		c.clearWidgets(collectionName);
	}
	/**
	 * Verifies if the parent of 'target' is the widget 'source'
	 */
	public static boolean isChild(Widget source, Element target) {

		if ( null == source )
			throw new RuntimeException(thisClass+".isChild: source can't be null");
		if ( null == target )
			throw new RuntimeException(thisClass+".isChild: target can't be null");
		
		Element src = source.getElement();
		Element parentTarget = DOM.getParent(target);
		
		return ( DOM.getElementProperty(src, "id") == DOM.getElementProperty(parentTarget, "id" ));
		
	}//
	
}//END